global class AT_WSLicenseUpdate 
{
    global class lu2Inputs
    {
        webservice String MacLicenseID;
        webservice String RegistrationDate;
        webservice String ProductID;
    }
    
    global class lu3Inputs
    {
        webservice String MacLicenseID;
        webservice String WinLicenseID;
        webservice String RegistrationDate;
        webservice String ProductID;
        webservice String ProductName;
        webservice String Comments;
        webservice String WinClientNumber;
        webservice String CouponCode;
        webservice String WinProductName;
    }
 
    // A class to send back as an output
    global class luOutputs
    {
        webservice boolean resultFlag;
    }
    
    webservice static luOutputs netUpdate2(lu2Inputs inputs)
    {
        String queryTxt = '';
        luOutputs output = new luOutputs();
        output.resultFlag = true;
        
        if(inputs!=null && (inputs.MacLicenseID!=null && inputs.MacLicenseID!='') && 
            (inputs.ProductID!=null && inputs.ProductID!=''))
        {
	        List<Licenses__c> items2Update = new List<Licenses__c>();

            queryTxt = 'Mac_License__c=\''+inputs.MacLicenseID+'\' and Product_Name__c=\''+inputs.ProductID+'\'';
            queryTxt  = 'select ID,Mac_License__c, Product_Name__c, Product__c, Registration_Date__c from Licenses__c where '+queryTxt;
            for(Licenses__c item:Database.query(queryTxt))
            {
                /*ID ProductID = null;
                for(Product2 prodItem:[Select Id,ProductCode From Product2 where ProductCode = :inputs.ProductID]){ ProductID = prodItem.Id;}
                item.Product__c = ProductID;*/
                
                String strProdID = inputs.ProductID; //ProductID
				if (strProdID.length() > 255) 
				{
    				strProdID = strProdID.substring(0,255);
				}
                
                item.Netupdate_Product__c = strProdID;
                
                if(inputs.RegistrationDate!=null && inputs.RegistrationDate!='')
                {
                    try{ item.Registration_Date__c = date.valueOf(inputs.RegistrationDate);}
                    catch(Exception ex){ item.Registration_Date__c = null;}
                }
                else item.Registration_Date__c = null;
                
                items2Update.add(item);
            }

    		if(items2Update.size()>0){ update items2Update;}
        }
        else output.resultFlag = false;

        return  output;
    }
    
    webservice static luOutputs netUpdate3(lu3Inputs inputs)
    {
        String queryTxt = '';
        luOutputs output = new luOutputs();
        output.resultFlag = true;
        

        if(inputs!=null && inputs.MacLicenseID!=null && inputs.MacLicenseID!='')
        {
			List<Licenses__c> items2Update = new List<Licenses__c>();
			
			List<Licenses__c> llResult = new List<Licenses__c>();

            queryTxt = 'Mac_License__c=\''+inputs.MacLicenseID+'\'';
            queryTxt  = 'select ID,Mac_License__c, Product_Name__c, Product__c, Registration_Date__c, Win_License__c, Comments__c, Win_Client_Number__c, DP_Coupon_Code__c, Win_Product_Name__c from Licenses__c where '+queryTxt;
            
            llResult = Database.query(queryTxt);
            
            if(llResult.Size()==0 && inputs.CouponCode!=null && inputs.CouponCode!='')
            {
            	queryTxt = 'Coupon__c=\''+inputs.CouponCode+'\'';
            	queryTxt  = 'select ID,Mac_License__c, Product_Name__c, Product__c, Registration_Date__c, Win_License__c, Comments__c, Win_Client_Number__c, DP_Coupon_Code__c, Win_Product_Name__c from Licenses__c where '+queryTxt;
            	llResult = Database.query(queryTxt);	
            }
            
            //for(Licenses__c item:Database.query(queryTxt))
            for(Licenses__c item:llResult)
            {
                /*ID ProductID = null;
                for(Product2 prodItem:[Select Id,ProductCode From Product2 where ProductCode = :inputs.ProductID]){ ProductID = prodItem.Id;}
                item.Product__c = ProductID;*/
                
                if(inputs.MacLicenseID == inputs.WinLicenseID){ item.Win_License__c = inputs.WinLicenseID;}
                
                if(inputs.RegistrationDate!=null && inputs.RegistrationDate!='')
                {
                    try{ item.Registration_Date__c = date.valueOf(inputs.RegistrationDate);}
                    catch(Exception ex){ item.Registration_Date__c = null;}
                }
                else item.Registration_Date__c = null;
                
                
                item.Netupdate_Product__c = inputs.ProductName;
                item.Netupdate_Product_ID__c = inputs.ProductID;
                item.Comments__c = inputs.Comments;
                item.Win_Client_Number__c = inputs.WinClientNumber;
                item.Coupon__c = inputs.CouponCode;
                item.Win_Product_Name__c = inputs.WinProductName;
                
                items2Update.add(item);
            }
    
    	    if(items2Update.size()>0){ update items2Update;}        
    
        }
        else output.resultFlag = false; 

        return  output;
    }
    
}